(function (aopc) {
    aopc.addExpandPage(812, function (get) {
        const Common = get('UtilsCommon', 'Common');
        return {
            expandInit() {
                const userinfo_dialog_view = this.getProperty('userinfo_dialog_view');
                this.user_datalist = this.getViewProperty(userinfo_dialog_view, 'user_datalist');
                this.username_list = this.getViewProperty(userinfo_dialog_view, 'username_list');
                this.user_datalist.changeChecked = this.userChangeChecked.bind(this);
                this.user_datalist.changeAllChecked = this.userChangeAllChecked.bind(this);
                this.keyword = this.getViewProperty(userinfo_dialog_view, 'keyword');
                this.keyword.valueChange = this.keywordChange.bind(this);
                this.depart_tree = this.getViewProperty(userinfo_dialog_view, 'depart_tree');
                this.depart_tree.selectNode = this.departSelectNode.bind(this);
                this.dialog_title = this.getViewProperty(userinfo_dialog_view, 'title');
                this.userinfo_dialog_view = userinfo_dialog_view;
                this.department_dialog_view = this.getProperty('department_dialog_view');
                this.user_dialog_view = this.getProperty('user_dialog_view');
            },
            saveUserCallback() {
                this.user_datalist.id = Common.createGuid();
                this.user_datalist.isCache = false;
                this.user_datalist.dataSource2 = null;
                this.user_datalist.refreshOptions();
                this.alertSuccess('保存成功！', () => this.user_dialog_view.setIsVisible(false));
            },
            saveDepartCallback() {
                this.depart_tree.id = Common.createGuid();
                this.depart_tree.isCache = false;
                this.depart_tree.refreshOptions();
                this.alertSuccess('保存成功！', () => this.department_dialog_view.setIsVisible(false));
            },
            showUsersDialog(property) {
                this.selectProperty = property;
                this.dialog_title.text = property.title;
                this.user_datalist.isMultiple = property.isMultiple;
                this.setUserNameDataSource(property.dataSource || []);
                if (property.dataSource && property.dataSource.length > 0) {
                    this.user_datalist.value = property.dataSource.map(m => m.UserId);
                }
                else this.user_datalist.value = [];
                if (this.user_datalist.dataSource2) this.user_datalist.dataSource = this.user_datalist.dataSource2;
                this.userinfo_dialog_view.setIsVisible(true);
            },
            departSelectNode(node) {
                this.selectDepartId = node.value === '0' ? '' : node.value;
                this.searchUser(this.keyword.getValue());
            },
            setSelectUsers() {
                this.selectProperty.dataSource = this.username_list.dataSource || [];
                if (this.selectProperty.setSelectUsers) this.selectProperty.setSelectUsers();

                this.userinfo_dialog_view.setIsVisible(false);
            },
            searchUser(keyword) {
                if (!this.user_datalist.dataSource2) this.user_datalist.dataSource2 = this.user_datalist.dataSource;

                let list = this.user_datalist.dataSource2;
                if (!Common.isNullOrEmpty(keyword)) list = list.filter(f => f.UserName2.indexOf(keyword) >= 0);
                if (this.selectDepartId) list = list.filter(f => f.DepartmentId === this.selectDepartId);

                this.user_datalist.dataSource = list;
                this.user_datalist.refreshOptions();
            },
            keywordChange(value) {
                if (!this.keyword.isChanged) return;
                this.searchUser(value);
            },
            userChangeChecked(m) {
                let dataSource = this.selectProperty.isMultiple ? this.username_list.dataSource || [] : [];

                if (m.checked) {
                    dataSource = dataSource.map(m => m);
                    dataSource.push(m.data);
                }
                else dataSource = dataSource.filter(f => f.UserId !== m.data.UserId);

                this.setUserNameDataSource(dataSource, true);
            },
            userChangeAllChecked(list, checked) {
                const list2 = list.map(m => m.data);
                let dataSource = this.selectProperty.isMultiple ? this.username_list.dataSource || [] : [];

                if (checked) {
                    dataSource = dataSource.map(m => m);
                    list2.forEach(m => {
                        if (!dataSource.some(d => d.UserId === m.UserId)) dataSource.push(m);
                    })
                }
                else dataSource = dataSource.filter(d => !list2.some(m => m.UserId === d.UserId));

                this.setUserNameDataSource(dataSource, true);
            },
            setUserNameDataSource(dataSource, isSet) {
                this.username_list.label = dataSource.length === 0 ? '' : '选中' + dataSource.length + '人';
                this.username_list.dataSource = dataSource;
                isSet && this.username_list.setValue && this.username_list.setValue(dataSource);
            }
        }
    });
})(window.aopc);